package com.mtnz.njk.Service;

import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.CountCommunityMapper;
import com.mtnz.njk.Mapper.IntegralMapper;
import com.mtnz.njk.Mapper.NjkUserMapper;
import com.mtnz.njk.entity.CountCommunityEntity;
import com.mtnz.njk.entity.IntegralEntity;
import com.mtnz.njk.entity.IntegralTypeEntity;
import com.mtnz.njk.entity.NjkUserEntity;
import com.mtnz.njk.model.IntegralShopModel;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @Description: 商品积分业务层
 * @Author: ZPX
 * @CreateDate: 2019\7\9 18:00
 * @Version: 1.0
 */
@Service
public class IntegralService {

    @Autowired
    IntegralMapper integralMapper;

    @Autowired
    NjkUserMapper njkUserMapper;

    @Autowired
    CountCommunityMapper countCommunityMapper;

    /**
     * 积分商品列表和详情
     *
     * @param pageSize
     * @param pageNum
     * @return
     * @throws Exception
     */
    public ResponseResult showListIntegral(@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum, Integer integralId, String userId) throws Exception {
        if (integralId != null) {
            IntegralEntity integralEntity = integralMapper.selectByPrimaryKey(integralId);
            return new ResponseResult(ResponseMessage.OK, integralEntity);
        }
        ArrayList<IntegralShopModel> integralShopModels = new ArrayList<>();
        PageHelper.startPage(pageSize, pageNum);
        // 查询积分商城中的商品
        List<IntegralEntity> integrals = integralMapper.showListIntegral();
        PageInfo<IntegralEntity> integralPageInfo = new PageInfo<>(integrals);
        // 查询有积分商城商品种类
        List<IntegralTypeEntity> integralTypeEntities = integralMapper.listIntegralType();
        List<IntegralEntity> integralShops = integralPageInfo.getList();
        for (IntegralTypeEntity typeEntity : integralTypeEntities) {
            IntegralShopModel integralShopModel = new IntegralShopModel();

            ArrayList<IntegralEntity> integralEntities = new ArrayList<>();
            for (IntegralEntity integralShop : integralShops) {
                if (typeEntity.getIntegralTypeId().equals(integralShop.getIntegralType())) {
                    integralShop.setUrl("http://www.meitiannongzi.com/nongjike/static/jsp/integral.html?productId=" + integralShop.getIntegralId());
                    integralShopModel.setTypeName(typeEntity.getIntegralTypeName());
                    integralEntities.add(integralShop);
                }
            }
            integralShopModel.setIntegralEntities(integralEntities);
            integralShopModels.add(integralShopModel);
        }
        // 移除列表为null的情况
        Iterator<IntegralShopModel> iterator = integralShopModels.iterator();
        while (iterator.hasNext()) {
            IntegralShopModel next = iterator.next();
            if ((next.getIntegralEntities() == null || next.getIntegralEntities().size() == 0) && next.getTypeName() == null) {
                iterator.remove();
            }
        }
        // 查询用户基本信息
        NjkUserEntity userEntity = njkUserMapper.selectByPrimaryKey(userId);
        HashMap<String, Object> mm = new HashMap<>();
        mm.put("integralShopModels", integralShopModels);
        mm.put("userEntity", userEntity);
        mm.put("pages", integralPageInfo.getPages());
        return new ResponseResult(ResponseMessage.OK, mm);
    }

    /**
     * 兑换积分商品
     *
     * @param userId
     * @param tradNum
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult tradIntegralShop(String userId, Integer tradNum, Integer integralId) throws Exception {
        // 查询该积分商品基本信息
        IntegralEntity integralEntity = integralMapper.selectByPrimaryKey(integralId);
        // 库存不足
        if (integralEntity != null && integralEntity.getRepositoryNum() < tradNum) {
            return new ResponseResult(ResponseMessage.FAIL, "商品库存不足");
        }
        // 查询该用户的基本信息
        NjkUserEntity userEntity = njkUserMapper.selectByPrimaryKey(userId);
        // 用户积分不足
        if (userEntity != null && userEntity.getIntegral() < tradNum * integralEntity.getIntegralSco()) {
            return new ResponseResult(ResponseMessage.FAIL, "您的积分不足");
        }
        // 兑换成功，减去积分
        userEntity.setIntegral(userEntity.getIntegral() - tradNum * integralEntity.getIntegralSco());
        // 修改用户积分
        njkUserMapper.updateByPrimaryKeySelective(userEntity);
        // 兑换成功，减去库存
        integralEntity.setRepositoryNum(integralEntity.getRepositoryNum() - tradNum);
        // 修改库存
        integralMapper.updateByPrimaryKeySelective(integralEntity);
        // 记录一条该用户积分兑换商品记录
        CountCommunityEntity countCommunityEntity = new CountCommunityEntity();
        countCommunityEntity.setUserId(userId);
        countCommunityEntity.setCommunityId(integralId + "");
        countCommunityEntity.setCommunityType(6);
        countCommunityEntity.setNum(tradNum + "");
        countCommunityEntity.setCreateTime(new Date());
        countCommunityEntity.setOrderNum("");
        countCommunityEntity.setNews(integralEntity.getIntegralName());
        countCommunityEntity.setAudit(0);
        countCommunityMapper.insertSelective(countCommunityEntity);
        return new ResponseResult(ResponseMessage.OK, userEntity.getIntegral());
    }

    /**
     * 用户积分兑换记录
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public ResponseResult integralRecord(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<CountCommunityEntity> userIntegralRecord = integralMapper.getUserIntegralRecord(userId);
        for (CountCommunityEntity countCommunityEntity : userIntegralRecord) {
            // 积分商品id
            String shopId = countCommunityEntity.getCommunityId();
            IntegralEntity integralEntity = integralMapper.selectByPrimaryKey(shopId);
            countCommunityEntity.setCountIntegral(integralEntity.getIntegralSco() * Integer.valueOf(countCommunityEntity.getNum()));
        }
        PageInfo<CountCommunityEntity> countCommunityEntityPageInfo = new PageInfo<>(userIntegralRecord);
        return new ResponseResult(ResponseMessage.OK, countCommunityEntityPageInfo);
    }

}
